from django.db.models.fields import files
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from rest_framework.utils import field_mapping
from management.models import equipment, user, epRunning, scrap, repair, uploadFile


# 定义序列化器(转换、校验)

class uploadFileSerializer(serializers.ModelSerializer):
    """
    文件上传序列化器
    """
    #file = serializers.FileField( max_length=None, use_url=True, allow_null=True, required=False)
    
    class Meta:
        model = uploadFile
        fields = "__all__"

    def validate_file(self, data):
        fileName = data.name.split(".")
        if fileName[-1].lower() not in ["jpg", "jpeg", "png", "ico"]:
            raise ValidationError("文件格式异常！")  # 状态值400
        else:
            return data
            


class epSerializer(serializers.ModelSerializer):
    """
    设备表序列化
    """
    # 设置日期格式化格式,解决取出来的日期在json格式下带有“T”的问题
    # purchaseDate = serializers.DateField(format='%Y-%m-%d')
    batch = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')

    class Meta:
        model = equipment
        # read_only_fields = ['purchaseDate']
        fields = "__all__"


class in_userSerializer(serializers.ModelSerializer):
    """
    人员信息序列化
    """

    class Meta:
        model = user
        fields = "__all__"


class out_userSerializer(serializers.ModelSerializer):
    """
    人员信息序列化
    """
    pid = uploadFileSerializer(read_only=True)

    class Meta:
        model = user
        fields = ['id', 'type', 'pid']



class epSerializer1(serializers.ModelSerializer):
    """
    设备表序列化(部分属性)
    """

    class Meta:
        model = equipment
        # read_only_fields = ['purchaseDate']

        fields = ["id", "category", "deviceName", "model"]


class epRunningSerializer(serializers.ModelSerializer):
    """
    设备运行信息序列化
    """

    # eid = serializers.PrimaryKeyRelatedField(read_only=True)
    # eid_set = serializers.PrimaryKeyRelatedField(read_only=True)  # 新增
    eid = epSerializer1(read_only=True)

    # django.db.utils.IntegrityError: (1048, "Column 'eid_id' cannot be null")
    class Meta:
        model = epRunning
        fields = "__all__"

    def create(self, validated_data):
        epRunning.eid = validated_data['eid']
        epRunning.place = validated_data['place']
        epRunning.belongIn = validated_data['belongIn']
        epRunning.isActive = validated_data['isActive']
        epRunning.inUsing = validated_data['inUsing']
        epRunning.enableDate = validated_data['enableDate']
        epRunning.save()
        return epRunning


class repairSerializer(serializers.ModelSerializer):
    """
    维修信息序列化
    """
    eprid = epRunningSerializer(read_only=True)

    class Meta:
        model = repair
        fields = "__all__"


class repairSerializer1(serializers.ModelSerializer):
    """
    维修信息序列化
    """

    class Meta:
        model = repair
        fields = "__all__"


class scrapSerializer(serializers.ModelSerializer):
    """
    报废信息序列化
    """

    eid = epSerializer1(read_only=True)

    class Meta:
        model = scrap
        fields = "__all__"


class scrapSerializer1(serializers.ModelSerializer):
    """
    报废信息序列化
    """

    # epid = epSerializer1(read_only=True)
    # 上一行指定epid，会导致报错 django.db.utils.IntegrityError: (1048, "Column 'epid_id' cannot be null")

    class Meta:
        model = scrap
        fields = "__all__"


